let mta = require('../../utils/util');
let app = getApp();
Page({
    data: {
        header: true,
        pageIndex: 1,
        pageSize: 10,
        showArr: [],
        lastPage: false,
        urlAddress: getApp().globalData.imageUrl

    },

    onLoad: function(options) {
        console.log(options.code)

        if (options.code) {
            this.getMxConpons(options.code, options.id);
            this.setData({
                header: false
            })
        } else {
            this.getConpons(this.data.pageIndex, this.data.pageSize);
        }
        wx.setNavigationBarTitle({
            title: '优惠券详情' //页面标题为路由参数
        })

    },


    onReady: function() {
        // 生命周期函数--监听页面初次渲染完成

    },
    onShow: function() {
        // 生命周期函数--监听页面显示 
    },


    onHide: function() {
        // 生命周期函数--监听页面隐藏

    },
    onUnload: function() {
        // 生命周期函数--监听页面卸载

    },
    onPullDownRefresh: function() {
        // 页面相关事件处理函数--监听用户下拉动作

    },
    onReachBottom: function() {
        // 页面上拉触底事件的处理函数

        // if (!this.data.lastPage) {
        //     this.getConpons(this.data.pageIndex, this.data.pageSize);
        // }


    },
    onShareAppMessage: function() {
        // 用户点击右上角分享
        return {
            title: 'title', // 分享标题
            desc: 'desc', // 分享描述
            path: 'path' // 分享路径
        }
    },
    getMxConpons: function(id, code) {
        var _self = this;
        wx.request({
            url: getApp().globalData.baseUrl + '/applet/recharge/getItemmVouchers.do',
            data: {
                activityCode: code,
                itemId: id
            },
            header: getApp().globalData.header,
            success: function(res) {
                _self.data.pageIndex++;
                if (res.data.code == 200) {

                    if (res.data.obj != null && res.data.obj.length > 0) {
                        for (var i = 0; i < res.data.obj.length; i++) {
                            _self.data.showArr.push(res.data.obj[i]);
                        }
                    }
                    _self.setData({
                        showArr: _self.filterArr(_self.data.showArr)
                    })
                    console.log(_self.data.showArr)
                }

            }
        })
    },
    getConpons: function(index, size) {
        var _self = this;
        wx.request({
            url: getApp().globalData.baseUrl + '/applet/coupon/preVoucher.do',
            data: {
                shopCode: getApp().globalData.common.shopCode,
                pageIndex: index,
                pageSize: size,
            },
            header: getApp().globalData.header,
            success: function(res) {
                _self.data.pageIndex++;
                if (res.data.code == 200) {

                    if (res.data.obj.result != null && res.data.obj.result.length > 0) {
                        for (var i = 0; i < res.data.obj.result.length; i++) {
                            _self.data.showArr.push(res.data.obj.result[i]);
                        }
                    }
                    _self.setData({
                        lastPage: res.data.obj.lastPage,
                        pageIndex: _self.data.pageIndex,
                        showArr: _self.filterArr(_self.data.showArr)
                    })
                }

            }
        })
    },
    filterArr: function(arr) {
        for (var i = 0; i < arr.length; i++) {
            if (arr[i].type == "exclusive") {
                arr[i].moneyStr = "无限制"
            } else {
                arr[i].moneyStr = "满" + this.fitNum(arr[i].billAmountCondition) + "减" + this.fitNum(arr[i].presentAmount)
            }

            if (Date.parse(new Date()) > arr[i].sendEndTime) {
                arr[i].imagePath = "coupins_later";
                arr[i].b_color = "unomal_style";

            } else if (Date.parse(new Date()) < arr[i].sendEndTime && arr[i].draw) {
                arr[i].imagePath = "coupins_pass";
                arr[i].b_color = "unomal_style";
            } else if (Date.parse(new Date()) < arr[i].sendEndTime && !arr[i].draw) {
                arr[i].imagePath = "coupins_nows";
                arr[i].b_color = "nomal_style";
            }

            if (!arr[i].shopNames && !arr[i].goodNames) {
                // arr[i].shopRouls = "此优惠券为通用券；";
                arr[i].goodsRouls = "此优惠券为通用券；";

            } else {
                var str1 = "";
                if (arr[i].shopNames) {
                    str1 += "此优惠券只适用于指定门店："
                    for (var j = 0; j < arr[i].shopNames.length; j++) {
                        str1 += arr[i].shopNames[j];
                        str1 += "、";

                    }

                    str1 = str1.length > 0 ? str1.substring(0, str1.length - 1) + "" : "";
                    str1 += '；'

                } else {
                    str1 += "此优惠券适用于所有门店"
                }

                arr[i].shopRouls = str1;
                var str2 = '';
                if (arr[i].goodNames) {
                    str2 += "此优惠券只适用于指定商品："
                    for (var j = 0; j < arr[i].goodNames.length; j++) {
                        str2 += arr[i].goodNames[j];
                        str2 += "、";
                    }
                    str2 = str2.length > 0 ? str2.substring(0, str2.length - 1) + "" : "";
                } else {
                    str2 += "此优惠券适用于所有商品"
                }
                // arr[i].goodsRouls = str2;
                arr[i].goodsRouls = str1 + str2;
                if (arr[i].goodsRouls.length < 46) {
                    arr[i].rouleStyle = 'nomalRoule'
                } else {
                    arr[i].rouleStyle = 'shortRoule';
                    arr[i].transform_style = 'up_style'
                }
            }
        }
        return arr;
    },
    fitNum: function(num) {
        if (num) {
            var count = num.split(".")[0];
            var point = num.split(".")[1];
            if (parseInt(point) == 0) {
                return count;
            } else {
                return num;
            }
        }

    },

    showMyToast: function(self, my_toast_tip, time = 1000, template = 'my_toast') {
        self.setData({
            template: template, //my_toast
            my_toast_tip: my_toast_tip
        })
        var timer = setTimeout(() => {
            self.setData({
                template: '', //my_toast
                my_toast_tip: ''
            })
            clearTimeout(timer);
        }, time)

    },
    showDetail: function(e) {
        var id = e.target.dataset.code;
        console.log(this.data.showArr)
        for (var i = 0; i < this.data.showArr.length; i++) {
            if (id == this.data.showArr[i].voucherId) {
                if (this.data.showArr[i].rouleStyle == 'shortRoule') {
                    this.data.showArr[i].rouleStyle = 'longRoule'
                    this.data.showArr[i].transform_style = 'down_style'
                } else if (this.data.showArr[i].rouleStyle == 'longRoule') {
                    this.data.showArr[i].rouleStyle = 'shortRoule';
                    this.data.showArr[i].transform_style = 'up_style'
                }
            }
        }
        this.setData({
            showArr: this.data.showArr
        })
    }


})